﻿@inject IHttpService _httpService

@inject NavigationManager NavigationManager
@inject IPopupService PopupService

<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="发表文章"/>

<AutoSaveTip TItem="CreateArticleViewModel" Item="Model" DataLoaded="DataLoaded" @ref="autoSave" />


<div>
    <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Cards.EditSummaryTip Type="ExaminedNormalListModelType.Article" Class="mb-3" Creating/>
    @if (isReady == false)
    {
        <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />
    }
    else
    {
        <CnGalWebSite.Components.Containers.MasaTitleContainer Title="发表文章" Icon="@IconType.Article.ToIconString()" Outline>
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Articles.MainCard Model="Model.Main" Class="mb-4" />
         <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Articles.RelevancesCard Model="Model.Relevances" Class="mb-4" EntryGameItems="EntryGameItems" EntryRoleItems="EntryRoleItems" EntryGroupItems="EntryGroupItems" EntryStaffItems="EntryStaffItems" ArticleItems="ArticleItems" VideoItems="VideoItems" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Articles.MainPageCard Model="Model.MainPage" Class="mb-4" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Buttons.SubmitButton OnSubmit="OnSubmit" Model="Model" />
        </CnGalWebSite.Components.Containers.MasaTitleContainer>
    }

</div>

@code {
    private CreateArticleViewModel Model { get; set; } = new CreateArticleViewModel
        {
            Main = new EditArticleMainViewModel
            {
                PubishTime = DateTime.Now.ToCstTime()
            }
        };

    private bool isReady = false;
    private List<string> EntryGameItems { get; set; } = new List<string>();
    private List<string> EntryRoleItems { get; set; } = new List<string>();
    private List<string> EntryGroupItems { get; set; } = new List<string>();
    private List<string> EntryStaffItems { get; set; } = new List<string>();
    private List<string> ArticleItems { get; set; } = new List<string>();
    private List<string> VideoItems { get; set; } = new List<string>();

    private bool IsOnlyNeedPublishTime { get; set; } = true;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    AutoSaveTip<CreateArticleViewModel> autoSave { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                //获取所有词条
                EntryGameItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/0");
                EntryRoleItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/1");
                EntryGroupItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/2");
                EntryStaffItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/3");
                //获取所有文章
                ArticleItems = await _httpService.GetAsync<List<string>>("api/articles/GetAllArticles");
                //获取所有视频 
                VideoItems = await _httpService.GetAsync<List<string>>("api/videos/GetNames");
                isReady = true;
                StateHasChanged();
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取所有词条文章名称列表失败");
            }
        }
    }

    private async Task OnSubmit()
    {
        try
        {

            var obj = await _httpService.PostAsync<CreateArticleViewModel, Result>("api/articles/create", Model);
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("创建文章失败", obj.Error);
            }
            else
            {
                await autoSave.Clean();

                await PopupService.ToastSuccessAsync("创建文章成功", "创建文章成功，等待审核通过");
                NavigationManager.NavigateTo("/articles/index/" + obj.Error);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "创建文章失败");
        }
    }

    private void DataLoaded(CreateArticleViewModel model)
    {
        isReady = false;
        StateHasChanged();
        Model = model;
        isReady = true;
        StateHasChanged();
        
    }


}
